package bounjour;
import java.sql.*;
import java.util.*;
import java.util.Date;

public class Post extends IObjectFactory {
	
	private int id;
	public int getID() { return id; }

	private java.util.Date createDate;
	public java.util.Date getCreateDate() { return createDate; }

	private String body;
	public String getBody() { return body; }
	public void setBody(String value) { body = value; }

	private int userId;
	public int getUserID() { return userId; }
	public void setUserID(int value) { userId = value; }

	private String userName;
	public String getUserName() {
		return userName;
	}
	
	public void buildFromReader(java.sql.ResultSet RS) {
		try {
			this.id = RS.getInt("ID");
			this.createDate = RS.getDate("CreateDate");
			this.body = RS.getString("Body");
			this.userId = RS.getInt("UserID");
			try {
				this.userName = RS.getString("UserName");
			} catch (Exception e0) {}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public void save() {
		Connection cn = null;
		java.sql.PreparedStatement ps = null;
		
	    try{
	    	cn = getConnection();
	    	if (this.id == 0) { // create	    		
			    ps = cn.prepareStatement("INSERT INTO Posts (Body,UserID,CreateDate) VALUES (?,?,?)", Statement.RETURN_GENERATED_KEYS);
			    ps.setString(1, this.getBody());
			    ps.setInt(2, this.getUserID());
			    ps.setDate(3, new java.sql.Date(new Date().getTime()));
			    ps.execute();
			    ResultSet rs = ps.getGeneratedKeys();
			    if (rs.next()) this.id = rs.getInt(1);
			    rs.close();
	    	}
	    	else { // update
	    		ps = cn.prepareStatement("UPDATE Posts SET Body=?,UserID=?,CreateDate=?) WHERE ID=?");
			    ps.setString(1, this.getBody());
			    ps.setInt(2, this.getUserID());
			    ps.setDate(3, new java.sql.Date(new Date().getTime()));
			    ps.setInt(4, this.getID());
			    ps.execute();
	    	}
	    	cn.close();
	    	ps.close();
	    }
	    catch (Exception sqlEx){
	      System.err.println(sqlEx);
	    }			
	}
	
	public List<Comment> getComments() {
		return Comment.getComments(this.getID());
	}
	
	public List<Field> getFields() {
		List<Field> fields = Field.getFields(this.getID());
		if (fields == null || fields.size() == 0) {
			Field f = new Field();
			f.setType(FieldType.Text);
			f.setLabel("Comment");
			List<Field> ff = new ArrayList<Field>();
			ff.add(f);
			return ff;
		} else return fields;
	}
	
	public List<Tag> getTags() {
		return Tag.getTags(this.id);
	}
	
	static public List<Post> getPosts() {
		return getObjectList(Post.class, "SELECT Posts.*, Users.DisplayName AS UserName FROM Posts INNER JOIN Users ON Posts.UserID=Users.ID ORDER BY ID DESC;");
	}
	
	static public List<Post> getPosts(int userID) {
		return getObjectList(Post.class, "SELECT Posts.*, Users.DisplayName AS UserName FROM Posts INNER JOIN Users ON Posts.UserID=Users.ID WHERE Posts.UserID=? ORDER BY ID DESC;", userID);
	}
	
	static public List<Post> getPosts(String tag) {
		return getObjectList(Post.class, "select DISTINCT Posts.*, Users.DisplayName AS UserName, Tags.Name from Posts, PostTags, Tags, Users where Posts.ID = PostTags.`PostID` AND PostTags.TagID = Tags.ID AND Posts.UserID=Users.ID AND Tags.Name = ?", tag);
	}
	
	static public Post getPostByID(int id) {
		return getSingleObject(Post.class, "SELECT Posts.*, Users.DisplayName AS UserName FROM Posts LEFT OUTER JOIN Users ON Posts.UserID=Users.ID WHERE Posts.ID=?", id);
	}
	
}
